package com.sakiko.dailynote.controller;

import com.sakiko.dailynote.pojo.Article;
import com.sakiko.dailynote.pojo.PageBean;
import com.sakiko.dailynote.pojo.Result;
import com.sakiko.dailynote.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success("添加文章成功");
    }

    @GetMapping
    public Result<PageBean<Article>> getList(Integer pageNum,
                                             Integer pageSize,
                                             @RequestParam(required = false) Integer categoryId,
                                             @RequestParam(required = false) String state){
        PageBean<Article> pageBean = articleService.list( pageNum, pageSize, categoryId, state);
        return Result.success(pageBean);
    }

    @GetMapping("/detail")
    public Result getDetail(Integer id){
        Article article = articleService.getArticle(id);
        return Result.success(article);
    }

    @PutMapping
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return Result.success("更新文章详情成功");
    }

    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success("删除文章成功");
    }
}
